import * as api from '@/api/hmmm/questions'
import { simple as subSimple } from '@/api/hmmm/subjects'
import { simple as userSimple } from '@/api/base/users'
import { list as catalogList } from '@/api/hmmm/directorys'
import { list as tagsList } from '@/api/hmmm/tags'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { questionType, difficulty, direction, chkType} from '@/api/hmmm/constants'
import { parseTime } from '@/utils/index'
import previewDialog from '../../components/questions-preview'
import checkDialog from '../../components/questions-check'
import 'lodash'

export default {
    components: {
        previewDialog,checkDialog
    },
    data() {
        return {
            tableKey: 0,
            dataList: [],
            listLoading: false,
            total: 0,
            requestParameters: {
                page: 1,
                pagesize: 5,
            },
            alertText: '',
            citySelect: {
                province: [],
                cityData: []
            },
            subjectList: [],
            catalogData: [],
            tagList: [],
            subID: 0,
            userList: [],
            catalogList: [],
            formData: {},
            previewQes: false,
            activeName: 'first',
            chkDialog:false,
        }
    },
    computed: {
        questionType() { return questionType },
        difficulty() { return difficulty },
        direction() { return direction },
        chkType() { return chkType },
    },
    mounted() {
        this.initialDate()
    },
    methods: {
        // 初始数据
        initialDate() {
            // 读取数据
            this.getList()
            this.getSubjectList()
            this.getCityData()
            this.getUserList()
            this.getCatalogList()
        },
        // 每页显示信息条数
        handleSizeChange(val) {
            this.requestParameters.pagesize = val
            if (this.requestParameters.page === 1) {
                this.getList(this.requestParameters)
            }
        },
        // 进入某一页
        handleCurrentChange(val) {
            this.requestParameters.page = val
            this.getList()
        },
        // 当学科ID变化时
        handleSubjectChange(val) {
            delete this.requestParameters.catalogID
            delete this.requestParameters.tags
            this.getCatalogData(val)
            this.getTagsData(val)
        },
        // 重置
        resetForm() {
            this.$refs.requestParameters.resetFields()
            this.requestParameters = _.omitBy(this.requestParameters, (value, key) => _.isEmpty(value) && !_.isNumber(value))
        },
        // tab栏切换
        handleClick(e) {
            switch (e.index) {
                case '0':
                    delete this.requestParameters.chkState
                    break;
                case '1':
                    this.requestParameters.chkState = 0
                    break;
                case '2':
                    this.requestParameters.chkState = 1
                    break;
                case '3':
                    this.requestParameters.chkState = 2
                    break;
            }
            this.handleFilter()
        },

        /************搜索模块*****************/

        // 获取列表数据
        async getList() {
            this.listLoading = true
            const { data: res } = await api.choice(this.requestParameters)
            // 获取数据给dataList
            // console.log(res);
            this.dataList = res.items
            // 获取总条数
            this.total = res.counts
            this.alertText = `共 ${this.total} 条记录`
            this.listLoading = false
        },
        // 获取学科列表
        async getSubjectList() {
            const res = await subSimple()
            this.subjectList = res.data
        },
        // 获取目录列表
        async getCatalogList() {
            const { data: res } = await catalogList()
            this.catalogList = res.items
        },
        // 选学科获取二级目录
        getCatalogData(id) {
            this.catalogData = this.catalogList.filter(item => item.subjectID === id)
        },
        // 获取标签列表
        async getTagsData(id) {
            const { data: res } = await tagsList({ subjectID: id })
            this.tagList = res.items
        },
        // 获取用户列表
        async getUserList() {
            const res = await userSimple()
            this.userList = res.data
        },
        // 获取省
        getCityData() {
            this.citySelect.province = provinces()
        },
        // 选省获取到市
        handleProvince(e) {
            delete this.requestParameters.city
            this.citySelect.cityData = citys(e)
        },
        // 搜索信息
        handleFilter() {
            this.requestParameters.page = 1
            this.getList(this.requestParameters)
        },

        /************数据格式化***************/

        // 格式化学科
        formatterSubject(row, column, cellValue, index) {
            const item = this.subjectList.find(item => item.value === cellValue)
            return item ? item.label : ''
        },
        // 格式化目录
        formatterCatalog(row, column, cellValue, index) {
            const item = this.catalogList.find(item => item.id == cellValue)
            return item ? item.directoryName : ''
        },
        // 格式化题型
        formatterType(row, column, cellValue, index) {
            const item = questionType.find(item => item.value == cellValue)
            return item ? item.label : ''
        },
        // 格式化难度
        formatterDiff(row, column, cellValue, index) {
            const item = difficulty.find(item => item.value == cellValue)
            return item ? item.label : ''
        },
        // 格式化时间
        formatterTime(row, column, cellValue, index) {
            // return moment(cellValue).format('YYYY-MM-DD hh-mm-ss')
            return parseTime(cellValue)
        },
        // 格式化题干
        formatterQuestion(row, column, cellValue, index) {
            return cellValue.replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi, '').replace(/<[^>]+?>/g, '').replace(/\s+/g, ' ').replace(/ /g, ' ').replace(/>/g, ' ')
        },
        // 格式化审核状态
        formatterChkState(row, column, cellValue, index) {
            const item = chkType.find(item => item.value == (cellValue + 1))
            return item ? item.label : ''
        },
        // 格式化发布状态
        formatterPub(row, column, cellValue, index) {
            switch (cellValue) {
                case 0:
                    return '已下架'
                case 1:
                    return '已发布'
                default:
                    return '待发布'
            }
        },
        /*************列表操作*********************/
        //取消加入精选题库
        choiceQuestion(id) {
            this.$confirm('此操作将该题目从精选题库删除' + ', 是否继续?', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    await api.choiceAdd({ id: id, choiceState: 0 })
                        .then(response => {
                            this.$message.success('删除精选成功' + '!')
                            this.getList(this.requestParameters)
                        })
                        .catch(response => {
                            this.$message.error('删除精选失败!')
                        })
                })
                .catch(() => {
                    this.$message.info('已取消操作!')
                })
        },
        //改变发布状态
        changePublish(obj) {
            this.$confirm(`您确认${obj.publishState===0?'上架':'下架'}这道题目吗` + ', 是否继续?', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    await api.choicePublish({ id: obj.id, publishState: obj.publishState===0?1:0 })
                        .then(response => {
                            this.$message.success(`${obj.publishState===0?'上架':'下架'}成功` + '!')
                            this.getList(this.requestParameters)
                        })
                        .catch(response => {
                            this.$message.error(`${obj.publishState===0?'上架':'下架'}失败!`)
                        })
                })
                .catch(() => {
                    this.$message.info('已取消操作!')
                })
        },
        /*****************窗口操作*************************/
        // 表单详情数据加载
        async hanldePreviewQuestion(objeditId) {
            this.formData.id = objeditId
            const { data: res } = await api.detail({ id: objeditId })
            // 获取详情 给formData
            this.formData = res
            this.previewQes = true
            this.$nextTick(() => {
                this.$refs.previewQuestion.dialogFormV()
            })
        },
        // 审核表单详情数据加载
        hanldeCheckState(id) {
            this.formData.id = id
            this.chkDialog = true
            this.$nextTick(() => {
                this.$refs.checkStateRef.dialogFormV()
            })
        },
        // 试题审核
        async checkState(data) {
            data.chkState = Number(data.chkState)
            await api.choiceCheck(data).then(response => {
                this.$message.success('提交审核成功' + '!')
                this.getList()
            })
            .catch(response => {
                this.$message.error('提交审核失败!')
            })
        }
    }
}